package com.saifan.wyy_ov.ui.property;

import android.Manifest;
import android.app.Activity;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.provider.ContactsContract;
import android.provider.MediaStore;

import android.support.annotation.NonNull;
import android.support.v4.app.ActivityCompat;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.google.zxing.WriterException;
import com.saifan.wyy_ov.R;
import com.saifan.wyy_ov.ui.home.MainActivity;

import com.saifan.wyy_ov.ui.view.BaseActivity;
import com.saifan.wyy_ov.utils.QrCodeUtil;
import com.saifan.wyy_ov.utils.ScreenShotUtil;

/**
 * @version V1.0.0
 * @author: lomasky
 * @date: 2015-5-26 15:41:46
 * @descrption 生成二维码
 */

public class GenerateQRActivity extends BaseActivity {
    private Toolbar toolbar;
    private ImageView qr;
    private TextView contacts;
    private TextView tel;
    private TextView timer;
    private TextView date;
    private Bitmap bmp;
    String qrString;
    String timerString;
    String contactsString;
    String telString;
    String dateString;
    private LinearLayout buttonlayout;
    private TextView tip;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        qrString = getIntent().getStringExtra("qr");
        timerString = getIntent().getStringExtra("timer");
        contactsString = getIntent().getStringExtra("contacts");
        telString = getIntent().getStringExtra("tel");
        dateString = getIntent().getStringExtra("date");

        setContentView();

    }

    public void setContentView() {
        setContentView(R.layout.activity_generate_qr);
        buttonlayout = (LinearLayout) findViewById(R.id.buttonLayout);
        this.tip = (TextView) findViewById(R.id.tip);
        this.date = (TextView) findViewById(R.id.date);
        this.timer = (TextView) findViewById(R.id.timer);
        this.tel = (TextView) findViewById(R.id.tel);
        this.contacts = (TextView) findViewById(R.id.contacts);
        this.qr = (ImageView) findViewById(R.id.qr);
        this.toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        tel.setText(telString);
        contacts.setText(contactsString);

        if (timerString.equals("99999")) {
            timer.setText("不限");
        } else {
            timer.setText(timerString);
        }

        date.setText(dateString);
        int w = getWindowManager().getDefaultDisplay().getWidth();
        try {
            bmp = QrCodeUtil.getQrCodeImage(w / 2, w / 2, qrString);
            qr.setImageBitmap(bmp);
        } catch (WriterException e) {
            e.printStackTrace();
        }

    }

    //发给朋友
    public void sendToFriend(View view) {

        buttonlayout.setVisibility(View.GONE);
        tip.setText("请将此二维码在门岗处出示通行，请注意有效时间和有效次数");
        Bitmap bitmap = ScreenShotUtil.takeScreenShot(this);
        if (bitmap != null) {
            shareImage(bitmap);
        }
        tip.setText("将此二维码发送给来访人员，在门岗出示二维码即可");
        buttonlayout.setVisibility(View.VISIBLE);
    }

    //分享
    public void shareImage(Bitmap bitmap) {

        if (Build.VERSION.SDK_INT >= 23) {
            int check = ActivityCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE);
            if (check != PackageManager.PERMISSION_GRANTED) {
                ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, 1);
                return;
            } else {
                share(bitmap);
            }
        } else {
            share(bitmap);
        }


    }

    private void share(Bitmap bitmap) {
        try {
            Uri uriToImage = Uri.parse(MediaStore.Images.Media.insertImage(
                    getContentResolver(), bitmap, null, null));
            if (uriToImage!=null){
                Intent shareIntent = new Intent();
                shareIntent.setAction(Intent.ACTION_SEND);
                shareIntent.putExtra(Intent.EXTRA_STREAM, uriToImage);
                shareIntent.setType("image/*");
                startActivity(shareIntent);
            }



        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    public void goHome(View view) {
        startActivity(MainActivity.class);
        finish();
    }


}
